package com.lh.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.lang.math.RandomUtils;

/**
 * @author liuhui
 * @version 1.0
 * @date 2013-6-2 下午6:28:09
 */
public class ValidateCodeImgUtil {


	private static char[] CAPTCHARS = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8' };

	
	public static String generate(OutputStream out) throws Exception {

		int width = 85;
		int height = 35;

		int car = CAPTCHARS.length - 1;
		/**
		 * 产生随机字符串
		 */
		String code = "";
		for (int i = 0; i < 4; i++) {
			code += CAPTCHARS[RandomUtils.nextInt(car) + 1];
		}

		BufferedImage image = new BufferedImage(width , height, BufferedImage.TYPE_BYTE_INDEXED);

		Graphics2D graphics = image.createGraphics();

		/**
		 * 设置背景色
		 */
		graphics.setColor(Color.white);

		graphics.fillRect(0, 0, image.getWidth(), image.getHeight());

		graphics.setColor(Color.black);
		graphics.setFont(new Font("Book antiqua", Font.BOLD, 25));
		graphics.setColor(new Color(54, 38, 18));
		graphics.drawString(code, 10, 24);
		drawThickLine(graphics, 0, RandomUtils.nextInt(height) + 1, width, RandomUtils.nextInt(height) + 1, 3, Color.BLACK);
//		int w = image.getWidth();
//		int h = image.getHeight();
		shear(graphics, width, height, Color.white);
		graphics.dispose();
		ImageIO.write(image, "jpeg", out);
		return code;
	}

	private static void shear(Graphics g, int w1, int h1, Color color) {

		shearX(g, w1, h1, color);
		shearY(g, w1, h1, color);
	}

	private static void shearX(Graphics g, int w1, int h1, Color color) {

		int period = RandomUtils.nextInt(2);

		boolean borderGap = true;
		int frames = 1;
		int phase = RandomUtils.nextInt(2);

		for (int i = 0; i < h1; i++) {
			double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (Math.PI * 2 * (double) phase) / (double) frames);
			g.copyArea(0, i, w1, 1, (int) d, 0);
			if (borderGap) {
				g.setColor(color);
				g.drawLine((int) d, i, 0, i);
				g.drawLine((int) d + w1, i, w1, i);
			}
		}

	}

	private static void shearY(Graphics g, int w1, int h1, Color color) {

		int period = RandomUtils.nextInt(5) + 10; 

		boolean borderGap = true;
		int frames = 20;
		int phase = 7;
		for (int i = 0; i < w1; i++) {
			double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (Math.PI * 2 * (double) phase) / (double) frames);
			g.copyArea(i, 0, 1, h1, 0, (int) d);
			if (borderGap) {
				g.setColor(color);
				g.drawLine(i, (int) d, i, 0);
				g.drawLine(i, (int) d + h1, i, h1);
			}

		}

	}

	private static void drawThickLine(Graphics g, int x1, int y1, int x2, int y2, int thickness, Color c) {

		// The thick line is in fact a filled polygon
		g.setColor(c);
		int dX = x2 - x1;
		int dY = y2 - y1;
		// line length
		double lineLength = Math.sqrt(dX * dX + dY * dY);

		double scale = (double) (thickness) / (2 * lineLength);

		// The x and y increments from an endpoint needed to create a
		// rectangle
		double ddx = -scale * (double) dY;
		double ddy = scale * (double) dX;
		ddx += (ddx > 0) ? 0.5 : -0.5;
		ddy += (ddy > 0) ? 0.5 : -0.5;
		int dx = (int) ddx;
		int dy = (int) ddy;

		// Now we can compute the corner points
		int xPoints[] = new int[4];
		int yPoints[] = new int[4];

		xPoints[0] = x1 + dx;
		yPoints[0] = y1 + dy;
		xPoints[1] = x1 - dx;
		yPoints[1] = y1 - dy;
		xPoints[2] = x2 - dx;
		yPoints[2] = y2 - dy;
		xPoints[3] = x2 + dx;
		yPoints[3] = y2 + dy;

		g.fillPolygon(xPoints, yPoints, 4);
	}

	public static void main(String[] args) throws Exception {
		String s = ValidateCodeImgUtil.generate(new FileOutputStream( new File( "f:/vc/aaa.jpg" )));
		System.out.println(s);
	}

}
